from OpenGL.arrays import vbo
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from numpy import array
from collections import namedtuple
import sys, math, os

##Point = namedtuple('POINT', ['x', 'y'], verbose=True)
Point = namedtuple('POINT3', ['x', 'y','z'], verbose=False)

# Tetraeder
p1 = Point(2,4,0) # 1
p2 = Point(0,0,0) # 2
p3 = Point(2,0,2) # 3 
p4 = Point(4,0,0) # 4

####points = [p1,p2,p3,p4,p1]
##points = [p1,p2,p3,p4,p1,p2]

vbo = vbo.VBO(array(points, 'f')) # vbo an Graka schicken

def init(width, height):
   """ Initialize an OpenGL windown and rendering context"""
   glClearColor(1, 1, 1, 1)         # background color
   glMatrixMode(GL_PROJECTION)      # switch to projection matrix
   glLoadIdentity()                 # set to 1
##   gluOrtho2D(-1, 15,               # links, rechts clipping planes
##              -1, 15)               # unten, oben  clipping planes
   gluPerspective(45.,              # oeffnungswinkel fovy
                  width/height,  # aspect ratio
                  0.1,           # near
                  100.           # far
                  )                  
   glMatrixMode(GL_MODELVIEW)       # switch to modelview matrix
   
   
def display():
   """Rendering einstellungen"""
   glClear(GL_COLOR_BUFFER_BIT,
           GL_DEPTH_BUFFER_BIT
           ) # resettet angegebenen Buffer
   glLoadIdentity()              # Matrix resetten
   gluLookAt(-10, 10, 5,           # eye                
             2, 0, 0,            # center
             0, 1, 0             # up
             )
   # Render Vertex Buffer Object
   # Rotiert wird gegen den Uhrzeigersinn CCW
##   glRotate(45,                  # Winkel
##            0, 1, 0              # Achse
##            )
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
   # Triangles
   vbo.bind()
   glVertexPointerf(vbo)   # defaultwerte verwenden
##   glVertexPointer(3,      # Anzahl Koordinaten pro Vertex (default 4)
##                   GL_FLOAT, # Datentyp der Koordinaten (default GL_FLOAT immer gut)
##                   0,      # stride (Byte-Abstand zum naechsten Anfangswert (default 0)
##                   vbo    # Pointer auf erstes Element erste Koordinate
##                   )
   glEnableClientState(GL_VERTEX_ARRAY)
   glColor3f(0,0,0)
   
   glDrawArrays(GL_TRIANGLE_STRIP,
                0, # anfangsindex
                5  # wieviele punkte
                )
   vbo.unbind()
   glDisableClientState(GL_VERTEX_ARRAY)
   
   # Flush commands  
   glFlush()  
   
 
def reshape():
   pass
   
def main():
   width, height = 500, 500
   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB )   
   glutInitWindowSize(width, height)
   glutCreateWindow("Test OPENGL Fensterchen")
   glutDisplayFunc(display)     #register display function

   
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

   init(width, height)#initialize OpenGL state and opengl context
   glutMainLoop()   #start even processing

if __name__ == "__main__":
   main()
